<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserDept extends Model
{
    use HasFactory;

    protected $guarded = [];

    public function child()
    {
        return $this->hasMany(UserDept::class, 'parentId');
    }

    public function buildTree()
    {
        return $this->with('children')->where(['parentId' => 0])->get();
    }

    public function children()
    {
        return $this->child()->with('children');
    }

    public function user()
    {
        return $this->hasMany(User::class, 'dept');
    }

}
